package com.sdf.com.emjdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by sanpi on 2016/7/3.
 */

public class PagerIndicatorView extends View {

    private Paint paint;
    private float radius;
    private int padding;
    private int mWidth;
    private int mHeight;

    public PagerIndicatorView(Context context) {
        this(context, null);
    }

    public PagerIndicatorView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PagerIndicatorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //初始化
        paint = new Paint();
        paint.setAntiAlias(true);

        //小圆点的半径
        float density = getResources().getDisplayMetrics().density;
        radius = 5 * density;
        //间距
        padding = (int) (5 * density);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //每一个圆的坐标
        float x = (mWidth - totalPage * radius * 2 - (totalPage - 1) * padding) / 2 + radius;
        float y = mHeight / 2;
        for (int i = 0; i < totalPage; i++) {
            if(i == currentPage){
                paint.setColor(Color.parseColor("#8C8C8C"));
            }else{
                paint.setColor(Color.parseColor("#BCBCBC"));
            }
            canvas.drawCircle(x, y, radius, paint);
            //移动坐标
            x += radius * 2 + padding;
        }
    }

    private int totalPage;
    private int currentPage;

    public void initData(int totalPage, int currentPage) {
        this.totalPage = totalPage;
        this.currentPage = currentPage;
    }
    public void notifiyDataSetChanged(int totalPage, int currentPage) {
        this.totalPage = totalPage;
        this.currentPage = currentPage;
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }
}
